<#include "/java_copyright.include">
<#assign className = table.className>   
<#assign tableName = table.sqlName>   
<#assign classNameLower = className?uncap_first>
<#assign flag=0>
package ${basepackage}.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

<#include "/java_imports.include">

public class ${className}Dao {
	
	public boolean insert(Connection conn,${className} ${classNameLower}){
		String sql = "INSERT INTO ${tableName }(<#list table.columns as column><#if flag!=0>,</#if>${column.sqlName}<#assign flag = flag+1/></#list>) <#assign flag=0 />values(<#list table.columns as column><#if flag!=0>,</#if>?<#assign flag = flag+1/></#list>)";
		PreparedStatement ps = null;
		try{
			ps = conn.prepareStatement(sql);
			int i=1;
			<#list table.columns as column>
			ps.setObject(i++,${classNameLower}.get${column.columnName}());
			</#list>
			int result = ps.executeUpdate();
			return result==1;
		} catch(Exception e){
			throw new DaoException(e);
		}finally{
			CommUtil.close(ps);
		}
	}

<#assign flag=0>
	public boolean update(Connection conn,${className} ${classNameLower}){
		String sql = "UPDATE ${tableName } SET <#list table.columns as column><#if !column.pk><#if flag!=0>,</#if>${column.sqlName}=?<#assign flag = flag+1/></#if></#list> <#assign flag=0 /> WHERE <#list table.columns as column><#if column.pk><#if flag!=0> and</#if>${column.sqlName}=?<#assign flag = flag+1/></#if></#list>";
		PreparedStatement ps = null;
		try{
			ps = conn.prepareStatement(sql);
			int i=1;
			<#list table.columns as column>
			<#if !column.pk>
			ps.setObject(i++,${classNameLower}.get${column.columnName}());
			</#if>
			</#list>
			<#list table.columns as column>
			<#if column.pk>
			ps.setObject(i++,${classNameLower}.get${column.columnName}());
			</#if>
			</#list>
			int result = ps.executeUpdate();
			return result==1;
		} catch(Exception e){
			throw new DaoException(e);
		}finally{
			CommUtil.close(ps);
		}
	}
<#assign flag=0>
	public boolean delete(Connection conn,${className} ${classNameLower}){
		String sql = "DELETE FROM ${tableName } WHERE <#list table.columns as column><#if column.pk><#if flag!=0> and</#if>${column.sqlName}=?<#assign flag = flag+1/></#if></#list>";
		PreparedStatement ps = null;
		try{
			ps = conn.prepareStatement(sql);
			int i=1;
			<#list table.columns as column>
			<#if column.pk>
			ps.setObject(i++,${classNameLower}.get${column.columnName}());
			</#if>
			</#list>
			int result = ps.executeUpdate();
			return result==1;
		} catch(Exception e){
			throw new DaoException(e);
		}finally{
			CommUtil.close(ps);
		}
	}
	
	public ${className} get(Connection conn,Integer id){
		String where = " id=?";
		Object[] args = new Object[]{id};
		List<${className}> list = list(conn ,where,args);
		if(list!=null&&list.size()!=0){
			return list.get(0);
		}
		return null;
	}
	
	public List<${className}> list(Connection conn){
		return list(conn ,null,null);
	}
	
	private List<${className}> list(Connection conn,String where,Object[] args){
		List<${className}> list = new ArrayList<${className}>();
		StringBuffer sql = new StringBuffer("SELECT * FROM ${tableName } WHERE 1=1");
		if(where != null){
			sql.append(where);
		}
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			ps = conn.prepareStatement(sql.toString());
			for(int i=0;i!=args.length;i++){
				ps.setObject(i+1,args[i]);
			}
			rs = ps.executeQuery();
			while(rs.next()){
				${className} ${classNameLower} = new ${className}();
				<#list table.columns as column>
				${classNameLower}.set${column.columnName}(rs.get${column.simpleJavaType}("${column.sqlName}"));
				</#list>
				list.add(${classNameLower});
			}
			return list;
		} catch(Exception e){
			throw new DaoException(e);
		}finally{
			CommUtil.close(ps);
			CommUtil.close(rs);
		}
	}
}
